﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace QuanLyBepAnTuThien
{
    public partial class UcThongKeThuChi : UserControl
    {
        public UcThongKeThuChi()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        bool thongke = true;
        bool thongketatca = true;
        private void rdthongkethu_CheckedChanged(object sender, EventArgs e)
        {
            if (rdthongkethu.Checked == true)
            {
                thongke = true;
            }
            else
            {
                thongke = false;
            }
        }

        private void cobthongke_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cobthongke.Text=="Tất Cả")
            {
                dtTuNgay.Enabled = false;
                dtDenNgay.Enabled = false;
                thongketatca = true;
            }
            else if (cobthongke.Text == "Trong Khoảng Thời Gian")
            {
                dtDenNgay.Enabled = true;
                dtTuNgay.Enabled = true;
                thongketatca = false;
            }
            else
            {
            }

        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (thongke == true)
            {
                if (thongketatca == false)
                {
                    SqlCommand cmd = new SqlCommand("ThongKe_Thu_TheoNgay", cn.ketnoi());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("tungay", SqlDbType.DateTime)).Value = dtTuNgay.Value;
                    cmd.Parameters.Add(new SqlParameter("denngay", SqlDbType.DateTime)).Value = dtDenNgay.Value;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    gridControl.DataSource = dt;
                    try
                    {
                        SqlCommand cmd_tong = new SqlCommand("ThongKe_TongThuTheoNgay", cn.ketnoi());
                        cmd_tong.CommandType = CommandType.StoredProcedure;
                        cmd_tong.Parameters.Add(new SqlParameter("tungay", SqlDbType.DateTime)).Value = dtTuNgay.Value;
                        cmd_tong.Parameters.Add(new SqlParameter("denngay", SqlDbType.DateTime)).Value = dtDenNgay.Value;
                        object ob = cmd_tong.ExecuteScalar();
                        lbtongthu.Text = String.Format("{0:N0}", Convert.ToDouble(ob));
                    }
                    catch
                    {
                        lbtongthu.Text = "0";
                    }
                }
                else
                {
                    gridControl.DataSource = cn.getDataTable("ThongKeTatCa_Thu");
                }
            }
        }   

    }
}
